-- Copyright 2012 Martti Merilaid, Nicolas BARBOTIN
-- This file is part of LuaTanks.
-- 
-- LuaTanks is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- 
-- LuaTanks is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
-- 
-- You should have received a copy of the GNU General Public License
-- along with LuaTanks.  If not, see <http://www.gnu.org/licenses/>.

LTanks = LTanks or {}
LTanks.Contest = -1
LTanks.InvitationWin = nil

net.Receive("LuaTanks", function(len)
	local msgId = net.ReadInt(8)

	if msgId == LTanks.MSG_START then
		local contestId = net.ReadInt(4)
		if contestId < 0 then return end

		LTanks.Contest = contestId
		local from = net.ReadEntity()
		local maxTime = net.ReadInt(16)

		if !from or !from:IsValid() or !from:IsPlayer() or !maxTime then return end

		LTanks.InvitationWin = vgui.Create("DFrame") -- Yes I know, I'm really not good at VGUI
		LTanks.InvitationWin:SetDeleteOnClose(true)
		LTanks.InvitationWin:ShowCloseButton(false)
		LTanks.InvitationWin:SetTitle("LuaTanks Contest")
		LTanks.InvitationWin:SetBackgroundBlur(true)

		local layout = vgui.Create("DListLayout", LTanks.InvitationWin)
		layout:SetPos(5, 30)

		local label = vgui.Create("DLabel", layout)
		
		if from:SteamID() == LocalPlayer():SteamID() then
			layout:SetSize(390, 80)
			LTanks.InvitationWin:SetSize(400, 105)
			label:SetText("Waiting for other players...")

			local progBar = vgui.Create("DProgress", layout) -- This progress bar might be deleted.... we'll see
			progBar:SetFraction(0)

			local cancelBtn = vgui.Create("DButton", layout)
			cancelBtn:SetText("Cancel")

			cancelBtn.DoClick = function()
				net.Start("LuaTanks") -- Tell the server to stop the contest
				net.WriteInt(LTanks.MSG_CANCEL_CONTEST, 8)
				net.WriteInt(contestId, 4)
				net.SendToServer()
				LTanks.InvitationWin:Close()
				LTanks.InitationWin = nil
			end
		else
			layout:SetSize(390, 55)
			LTanks.InvitationWin:SetSize(400, 80)
			label:SetText(from:GetName() .. " invited you to a LuaTanks contest. Accept invitation?")

			local subLayout = vgui.Create("DGrid", layout)
			local yesButton = vgui.Create("DButton")
			local noButton = vgui.Create("DButton")

			subLayout:SetCols(2)
			subLayout:SetColWide(80)
			subLayout:SetSize(190, 30)
			yesButton:SetText("Yes")
			noButton:SetText("No")

			noButton.DoClick = function()
				LTanks.InvitationWin:Close() -- /!\ I have to change this

				if LTanks.InitationWin then
					LTanks.InitationWin = nil
				end
			end

			subLayout:AddItem(yesButton)
			subLayout:AddItem(noButton)
		end

		LTanks.InvitationWin:Center()
		LTanks.InvitationWin:Show()
		LTanks.InvitationWin:MakePopup()
	elseif msgId == LTanks.MSG_CANCEL_CONTEST then
		if LTanks.InitationWin then -- Close window
			LTanks.InitationWin:Close()
			LTanks.InitationWin = nil
		else
			-- Temporary, Just to track bugs.
			MsgN("[LuaTanks] WARNING: Window not found!")
		end
	else
		-- Unknown message id !
		MsgN("[LuaTanks] WARNING: Received unknown net message " .. tostring(msgId))
	end
end)